package com.sw.bbs.board.controller;

import com.sw.bbs.board.dao.media.TagMapper;
import com.sw.bbs.board.pojo.Board;
import com.sw.bbs.board.pojo.Tag;
import com.sw.bbs.board.service.BoardService;
import com.sw.bbs.board.service.IndexService;
import com.sw.bbs.board.service.PostService;
import com.sw.bbs.board.vo.request.IndexSearchRequest;
import com.sw.bbs.board.vo.response.FollowBoardPostResponse;
import com.sw.bbs.board.vo.response.IndexSearchResponse;
import com.sw.bbs.board.vo.response.post.PostListResponse;
import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.common.common.ServiceException;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Desc: No Desc
 * @Author: wusonghe
 * @Date: 2019/4/1 14:07
 */

@Api(value = "首页接口模块",description = "首页接口", tags = {"Index Module"})
@RequestMapping("/index/")
@RestController
@Slf4j
public class IndexController {

    @Autowired
    private BoardService boardService;

    @Autowired
    private PostService postService;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private IndexService indexService;

    @GetMapping("/hotBoard")
    @ApiOperation(value = "查询热门板块接口", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "板块结构", response = ResponseEntity.class)})
    public ResponseEntity<?> hotBoard(HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            List<Board> list = boardService.queryHotlist();
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/index/hotBoard", e);
            throw new GlobalException("/index/hotBoard",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }
    }

    @GetMapping("/hotPost")
    @ApiOperation(value = "查询热门帖子接口", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "帖子结构", response = ResponseEntity.class)})
    public ResponseEntity<?> hotPost(HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            List<PostListResponse> list = postService.queryHotPostList();
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/index/hotPost", e);
            throw new GlobalException("/index/hotPost",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }
    }

    @GetMapping("/hotTag")
    @ApiOperation(value = "查询热门标签接口", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "热门标签", response = ResponseEntity.class)})
    public ResponseEntity<?> hotTag()
            throws GlobalException {
        try {
            List<Tag> list = tagMapper.queryHotList();
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/index/hotTag", e);
            throw new GlobalException("/index/hotTag",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }
    }
    @GetMapping("/search")
    @ApiOperation(value = "搜索全网接口", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "热门标签", response = ResponseEntity.class)})
    public ResponseEntity<?> search(@ApiParam @ModelAttribute IndexSearchRequest searchRequest)
            throws GlobalException {
        try {

            IndexSearchResponse searchResponse =  indexService.searchForAll(searchRequest);
            return new ResponseEntity<>(searchResponse, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/index/search", e);
            throw new GlobalException("/index/search",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }
    }
    @GetMapping("/followBoardPostList")
    @ApiOperation(value = "关注的板块的最新帖子接口", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "关注的板块的最新帖子", response = FollowBoardPostResponse.class)})
    public ResponseEntity<?> followBoardPostList(HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {

            List<FollowBoardPostResponse> followBoardPostResponseList = indexService.queryFollowBoardPost(httpServletRequest);
            return new ResponseEntity<>(followBoardPostResponseList, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/index/followBoardPostList", e);
            throw new GlobalException("/index/followBoardPostList",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }
    }

}
